package com.ruoyi.storage.service.impl;

import java.util.List;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.purchase.service.impl.AllocationDetailService;
import com.ruoyi.purchase.service.impl.AllocationPlanService;
import com.ruoyi.storage.domain.WarehouseInfo;
import com.ruoyi.storage.domain.WarehouseInventory;
import com.ruoyi.storage.mapper.WarehouseInfoMapper;
import com.ruoyi.storage.mapper.WarehouseInventoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.storage.mapper.DeliveryDetailMapper;
import com.ruoyi.storage.domain.DeliveryDetail;
import com.ruoyi.storage.service.IDeliveryDetailService;

import static ch.qos.logback.core.joran.action.ActionConst.NULL;

/**
 * 货品配送Service业务层处理
 * 
 * @author 庞一恺
 * @date 2025-02-23
 */
@Service
public class DeliveryDetailServiceImpl implements IDeliveryDetailService 
{
    @Autowired
    private DeliveryDetailMapper deliveryDetailMapper;
    @Autowired
    private WarehouseInventoryServiceImpl warehouseInventoryServiceImpl;
    @Autowired
    private WarehouseInfoServiceImpl warehouseInfoServiceImpl;
    @Autowired
    private WarehouseInfoMapper warehouseInfoMapper;
    @Autowired
    private WarehouseInventoryMapper warehouseInventoryMapper;
    @Autowired
    private AllocationPlanService allocationPlanService;

    /**
     * 查询货品配送
     * 
     * @param deliveryDetailId 货品配送主键
     * @return 货品配送
     */
    @Override
    public DeliveryDetail selectDeliveryDetailByDeliveryDetailId(String deliveryDetailId)
    {
        return deliveryDetailMapper.selectDeliveryDetailByDeliveryDetailId(deliveryDetailId);
    }

    /**
     * 查询货品配送列表
     * 
     * @param deliveryDetail 货品配送
     * @return 货品配送
     */
    @Override
    public List<DeliveryDetail> selectDeliveryDetailList(DeliveryDetail deliveryDetail)
    {
        return deliveryDetailMapper.selectDeliveryDetailList(deliveryDetail);
    }

    /**
     * 新增货品配送
     * 
     * @param deliveryDetail 货品配送
     * @return 结果
     */
    @Override
    public int insertDeliveryDetail(DeliveryDetail deliveryDetail)
    {
        return deliveryDetailMapper.insertDeliveryDetail(deliveryDetail);
    }

    /**
     * 修改货品配送
     * 
     * @param deliveryDetail 货品配送
     * @return 结果
     */
    @Override
    public int updateDeliveryDetail(DeliveryDetail deliveryDetail)
    {
        return deliveryDetailMapper.updateDeliveryDetail(deliveryDetail);
    }

    /**
     * 批量删除货品配送
     * 
     * @param deliveryDetailIds 需要删除的货品配送主键
     * @return 结果
     */
    @Override
    public int deleteDeliveryDetailByDeliveryDetailIds(String[] deliveryDetailIds)
    {
        return deliveryDetailMapper.deleteDeliveryDetailByDeliveryDetailIds(deliveryDetailIds);
    }

    /**
     * 删除货品配送信息
     * 
     * @param deliveryDetailId 货品配送主键
     * @return 结果
     */
    @Override
    public int deleteDeliveryDetailByDeliveryDetailId(String deliveryDetailId)
    {
        return deliveryDetailMapper.deleteDeliveryDetailByDeliveryDetailId(deliveryDetailId);
    }

    /**
     * 处理到货货品配送
     *
     * @param deliveryDetailIds 需要删除的货品配送主键
     * @return 结果
     */
    @Override
    public int arriveDeliveryDetailByDeliveryDetailIds(String[] deliveryDetailIds) {

        int result = 0;
        for (String deliveryDetailId : deliveryDetailIds){
//            更改状态
            DeliveryDetail deliveryDetail = selectDeliveryDetailByDeliveryDetailId(deliveryDetailId);
            deliveryDetail.setState("已送达");

            //生成待入库库存记录
            WarehouseInventory warehouseInventory = new WarehouseInventory();
            warehouseInventory.setBatchId(deliveryDetail.getProductBatch());
            warehouseInventory.setInventoryNum(deliveryDetail.getTransportQuantity());
            warehouseInventory.setWarehouseId(warehouseInfoMapper.getWarehouseIdByPrincipal(Math.toIntExact(SecurityUtils.getUserId())));
            //TODO warehouseInventory.setRawProductId();
            //TODO warehouseInventory.setGoodSpecification();
            warehouseInventory.setInventoryName(deliveryDetail.getProductName());
            warehouseInventory.setWeight(deliveryDetail.getWeight());
            warehouseInventory.setWeightUnit(deliveryDetail.getWeightUnit());
            warehouseInventory.setInventoryType("待配送入库");
            warehouseInventory.setZoneId(999999);
            warehouseInventory.setEvidenceRecordId(deliveryDetail.getDeliveryDetailId());

            warehouseInventory.setEvidenceRecordId(deliveryDetail.getDeliveryDetailId());

            //TODO 同步数据
            warehouseInventory.setOrigin("巴西");


            warehouseInventoryMapper.insertWarehouseInventory(warehouseInventory);
            result++;
            deliveryDetailMapper.updateDeliveryDetail(deliveryDetail);
        }

        return result;
    }
}
